package com.yr.ryjk.sysuser.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import com.yr.ryjk.common.ErrorType;
import com.yr.ryjk.common.Pagination;
import com.yr.ryjk.param.dao.ParamMapper;
import com.yr.ryjk.param.entity.Key;
import com.yr.ryjk.sysuser.dao.PermissionMapper;
import com.yr.ryjk.sysuser.dao.SysUserMapper;
import com.yr.ryjk.sysuser.entity.Permission;
import com.yr.ryjk.sysuser.entity.SuType;
import com.yr.ryjk.sysuser.entity.SysUser;
import com.yr.ryjk.util.IDGenerator;
import com.yr.ryjk.util.JSONUtil;

@Service
public class SuService implements ISuService{
    @Autowired
	private SysUserMapper sysuserMapper;
    @Autowired
    private ParamMapper paramMapper;
    @Autowired
    private PermissionMapper permissionMapper;
    
    /**
     * 实现分页功能
     */
	public Map<String, Object> getListOfPage(int pageNo, int pageSize,String suName,String suAddman) {
		List<SysUser> list=sysuserMapper.selectOfPage((pageNo-1)*pageSize,pageSize,suName,suAddman);
		int count=sysuserMapper.selectCount(suName,suAddman);
		
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("list", list);
		map.put("pagination", new Pagination(pageSize,pageNo,count));
		
		return map;
	}
	/**
	 * 添加系统用户
	 */
	@Override
	public Map<String, Object> addSysUser(SysUser sysuser) {
		SysUser su=sysuserMapper.selectByPrimaryKey(sysuser.getSuName());
		//如果用户存在
		if(su!=null){
			return JSONUtil.getJSON(ErrorType.SYSUSER_REPEATED_ERROR);
		}else{
			String defaultPassword=paramMapper.selectValByKey(Key.admin_default_password.getKey());
			//设置系统密码和系统时间
			sysuser.setSuPassword(DigestUtils.md5DigestAsHex(defaultPassword.getBytes()));
			sysuserMapper.insert(sysuser);
			if(sysuser.getSuLevel()==SuType.treasurer.getLevel()){ //财务添加默认权限
				Permission p=new Permission();
				p.setId(IDGenerator.gen32UUID());
				p.setPermissions("cashin,cashout");
				p.setSuName(sysuser.getSuName());
				
				permissionMapper.insert(p);
			}
			return JSONUtil.getJSON(ErrorType.SUCCESS);
		}
	}
	/**
	 * 根据用户名删除系统用户
	 */
	@Override
	public Map<String, Object> deleteSysUser(String suName) {
	    sysuserMapper.deleteByPrimaryKey(suName);
		return JSONUtil.getJSON(ErrorType.SUCCESS);
	}
	
	/**
	 * 更新系统用户的密码
	 */
	@Override
	public Map<String, Object> updateSysUserPw(String suName,String newpw) {
		sysuserMapper.updatePw(suName, newpw);
		return JSONUtil.getJSON(ErrorType.SUCCESS);
	}
	/**
	 * 登录
	 */
	@Override
	public SysUser getSysUserByNameAndPwd(String name, String pwd) {
		pwd=DigestUtils.md5DigestAsHex(pwd.getBytes());
		SysUser user=sysuserMapper.selectByNameAndPwd(name,pwd);
		if(user!=null){
			// 登陆成功时，更新当前管理员添加过的用户信息中最后登陆时间
			sysuserMapper.updateLastLoginDate(user.getSuName());
			return user;
		}else {
			return null;
		}	
	}
	/**
	 * 删除多个用户
	 */
	@Override
	public void deleteSysUsers(String suNames) {
		sysuserMapper.deleteBySuNames(suNames);
		permissionMapper.deleteBySuNames(suNames);
	}
	
	@Override
	public Permission getPermission(String suName) {
		return permissionMapper.selectBySuName(suName);
	}
	
	@Override
	public Map<String, Object> modifyPermission(String suName, String permissions) {
		Permission permission=permissionMapper.selectBySuName(suName);
		if(permission!=null){
			int i=permissionMapper.updateBySuName(suName,permissions);
			if(i==0){
				return JSONUtil.getJSON(ErrorType.SERVER_ERROR);
			}
		}else {
			permission=new Permission();
			permission.setId(IDGenerator.gen32UUID());
			permission.setPermissions(permissions);
			permission.setSuName(suName);
			
			permissionMapper.insert(permission);
		}
		return JSONUtil.getJSON(ErrorType.SUCCESS);
	}
}
